package com.linkoog.devtools.archive.filesystem.sevenzip;

import com.linkoog.devtools.archive.filesystem.ArchiveHandlerBase;
import com.intellij.util.io.FileAccessorCache;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;

/**
 */
public class SevenZipFileHandler extends ArchiveHandlerBase<SevenZipFile> {

    public SevenZipFileHandler(@NotNull String path) {
        super(path);
    }

    @Override
    public FileAccessorCache<SevenZipFileHandler, SevenZipFile> getFileAccessor() {
        return fileAccessor;
    }

    private static final FileAccessorCache<SevenZipFileHandler, SevenZipFile> fileAccessor =
            new FileAccessorCache<SevenZipFileHandler, SevenZipFile>(20, 10) {

                @Override
                protected SevenZipFile createAccessor(SevenZipFileHandler key) throws IOException {
                    setFileAttributes(key, key.getCanonicalPath());
                    return new SevenZipFile(key.getFile().getCanonicalFile());
                }

                @Override
                public boolean isEqual(SevenZipFileHandler val1, SevenZipFileHandler val2) {
                    return val1 == val2;
                }

                @Override
                protected void disposeAccessor(@NotNull SevenZipFile fileAccessor) throws IOException {
                    fileAccessor.close();
                }

            };

}